package utils;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtil {

    // 数据库配置
    private static String url;
    private static String username;
    private static String password;
    private static String driverClass;

    // 静态块加载配置文件
    static {
        try {
            // 读取配置文件 jdbc.properties
            InputStream input = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(input);

            // 获取配置
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            driverClass = properties.getProperty("driverClass");

            // 加载驱动
            Class.forName(driverClass);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("初始化数据库连接失败，请检查配置文件！");
        }
    }

    /**
     * 获取数据库连接
     *
     * @return Connection 数据库连接
     * @throws SQLException 数据库连接异常
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }

    /**
     * 关闭资源
     *
     * @param connection 数据库连接
     * @param statement  数据库操作对象
     * @param resultSet  查询结果集
     */
    public static void close(Connection connection, Statement statement, ResultSet resultSet) {
        try {
            if (resultSet != null) resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (statement != null) statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (connection != null) connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行更新操作（INSERT/UPDATE/DELETE）
     *
     * @param sql  SQL 语句
     * @param args 参数列表
     * @return 受影响的行数
     * @throws SQLException 数据库操作异常
     */
    public static int executeUpdate(String sql, Object... args) throws SQLException {
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }
            return preparedStatement.executeUpdate();
        }
    }

    /**
     * 执行查询操作
     *
     * @param sql  SQL 语句
     * @param args 参数列表
     * @return ResultSet 查询结果
     * @throws SQLException 数据库操作异常
     */
    public static ResultSet executeQuery(String sql, Object... args) throws SQLException {
        Connection connection = getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < args.length; i++) {
            preparedStatement.setObject(i + 1, args[i]);
        }
        return preparedStatement.executeQuery();
    }
}
